package design.simdi.code.final_paper.site.panels.licente.proiecte;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import design.simdi.code.final_paper.core.daos.ProjectDao;
import design.simdi.code.final_paper.core.daos.SesiuneDao;
import design.simdi.code.final_paper.core.entities.Project;
import design.simdi.code.final_paper.core.entities.Sesiune;
import design.simdi.code.final_paper.site.application.ApplicationRoles;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.PagePanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;

/**
 * 
 * @author Marius Dumitriu - (Jun 24, 2010 - 11:53:47 PM)
 * 
 */
public class ProiecteTabPanel extends PagePanel {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private ProjectDao projectDao;
	@SpringBean
	private SesiuneDao sesiuneDao;
	private DropDownChoice<Sesiune> sesiuniComboBox;
	private WebMarkupContainer tableContainer;

	public ProiecteTabPanel(String id) {
		super(id);
		InjectorHolder.getInjector().inject(this);
		((WicketApplication) getApplication()).setShowErrors(true);
	}

	private SortableProjectDataProvider sortableProjectDataProvider;

	@SuppressWarnings("unchecked")
	@Override
	public void panelInit() {
		sortableProjectDataProvider = new SortableProjectDataProvider();

		List<Sesiune> lista = getSesiuni();

		sesiuniComboBox = new DropDownChoice<Sesiune>("sesiuni-combo", lista);
		sesiuniComboBox.setNullValid(false);
		if (lista.size() > 0) {
			for (Sesiune s : lista)
				if (s.isDefaulta())
					sesiuniComboBox.setModel(new Model<Sesiune>(s));
			if (sesiuniComboBox.getModelObject() == null)
				sesiuniComboBox.setModel(new Model<Sesiune>(lista.get(0)));
			getSortableProjectDataProvider().getCriterii().add(
					Restrictions.eq("sesiune.id", sesiuniComboBox.getModelObject().getId()));

		}
		sesiuniComboBox.setOutputMarkupId(true);

		sesiuniComboBox.add(new AjaxFormComponentUpdatingBehavior("onchange") {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				onChangeSesiune(target);
			}
		});

		add(sesiuniComboBox);

		List<IColumn<Project>> columns = new ArrayList<IColumn<Project>>();

		columns.add(new PropertyColumn<Project>(new ResourceModel("coordinator"), "author", "author"));
		columns.add(new PropertyColumn<Project>(new ResourceModel("proiect"), "name", "name"));
		columns.add(new PropertyColumn<Project>(new ResourceModel("keywords"), "keywordsAsString"));
		// columns.add(new PropertyColumn<Project>(new ResourceModel("notes"),
		// "notes"));
		columns.add(new AbstractColumn<Project>(new ResourceModel("available"), "available") {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<Project>> cellItem, String componentId,
					IModel<Project> rowModel) {
				Project d = rowModel.getObject();
				Label l;
				if (d.isAvailable())
					l = new Label(componentId, new ResourceModel("da"));
				else
					l = new Label(componentId, new ResourceModel("nu"));
				cellItem.add(l);

			}
		});

		Roles roluriCurente = MyAuthenticatedWebSession.get().getRoles();
		if (MyAuthenticatedWebSession.get().isSignedIn()
				&& (roluriCurente.contains(ApplicationRoles.STUDENT) || roluriCurente
						.contains(ApplicationRoles.ABSOLVENT)))

			columns.add(new AbstractColumn<Project>(new ResourceModel("inscris")) {

				private static final long serialVersionUID = 1L;

				public void populateItem(Item<ICellPopulator<Project>> cellItem, String componentId,
						IModel<Project> rowModel) {
					Project d = rowModel.getObject();
					Label l;
					if ((getApplicationService().findStudentCandidatByProject(d, getCurrentPerson())) != null)
						l = new Label(componentId, new ResourceModel("da"));
					else
						l = new Label(componentId, new ResourceModel("nu"));
					cellItem.add(l);

				}
			});
		if (MyAuthenticatedWebSession.get().isSignedIn())
			if ((getCurrentPerson().getStudent() != null || getCurrentPerson().getAbsolvent() != null))
				;// am student sau absolvent.. ei tre sa vada toate domeniile
			else
				getSortableProjectDataProvider().setOwner(getCurrentPerson());
		getSortableProjectDataProvider().setSesiune(sesiuniComboBox.getModelObject());
		// getSortableProjectDataProvider().getCriterii().clear();
		// getSortableProjectDataProvider().getCriterii().add(
		// Restrictions.eq("sesiune.id",
		// sesiuniComboBox.getModelObject().getId()));
		MyDataTable<Project> table = new MyDataTable("table", columns, sortableProjectDataProvider, getRowsPerPage()) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public ActionToolbar getActionToolbar() {
				return new ProjectsActionToolbar(this);
			}
		};
		tableContainer = new WebMarkupContainer("table-container");
		tableContainer.add(table);
		tableContainer.setOutputMarkupPlaceholderTag(true);

		add(tableContainer);
	}

	protected void onChangeSesiune(AjaxRequestTarget target) {
		// getSortableProjectDataProvider().getCriterii().clear();
		// getSortableProjectDataProvider().getCriterii().add(
		// Restrictions.eq("sesiune.id",
		// sesiuniComboBox.getModelObject().getId()));
		if (MyAuthenticatedWebSession.get().isSignedIn())
			if ((getCurrentPerson().getStudent() != null || getCurrentPerson().getAbsolvent() != null))
				;// am student sau absolvent.. ei tre sa vada toate domeniile
			else
				getSortableProjectDataProvider().setOwner(getCurrentPerson());
		getSortableProjectDataProvider().setSesiune(sesiuniComboBox.getModelObject());
		target.addComponent(tableContainer);

	}

	private List<Sesiune> getSesiuni() {
		List<Sesiune> lista = new ArrayList<Sesiune>();
		lista = sesiuneDao.findAll(new Order[] { Order.asc("name") });
		return lista;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setSesiuneDao(SesiuneDao sesiuneDao) {
		this.sesiuneDao = sesiuneDao;
	}

	public SesiuneDao getSesiuneDao() {
		return sesiuneDao;
	}

	public void setSortableProjectDataProvider(SortableProjectDataProvider sortableProjectDataProvider) {
		this.sortableProjectDataProvider = sortableProjectDataProvider;
	}

	public SortableProjectDataProvider getSortableProjectDataProvider() {
		return sortableProjectDataProvider;
	}
}